//! \file WindowDice.cpp
//! \brief TODO: Document
// -----------------------------------------------------------------------------
//! This program is free software; you can redistribute it and/or
//! modify it under the terms of the GNU General Public License
//! as published by the Free Software Foundation; either version 2
//! of the License, or (at your option) any later version.
//!
//! This program is distributed in the hope that it will be useful,
//! but WITHOUT ANY WARRANTY; without even the implied warranty of
//! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//! GNU General Public License for more details.
//!
//! You should have received a copy of the GNU General Public License
//! along with this program; if not, write to the Free Software Foundation,
//! Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// -----------------------------------------------------------------------------

#include "Elements.h"
#include "GuiCEGUI.h"
#include <stddef.h>

#include "Dice.h" // TODO: remove once rolled up to the DD library.

using namespace CEGUI;

// =====[ Local Definitions ]===================================================

// =====[ Local DataTypes ]=====================================================

// =====[ Local Variables ]=====================================================

// =====[ Local Functions ]=====================================================

// =====[ Private (Internal) Methods ]==========================================

// =====[ Local Implementation ]================================================








// Sample sub-class for ListboxTextItem that auto-sets the selection brush
// image.  This saves doing it manually every time in the code.
class MyListItem : public CEGUI::ListboxTextItem
{
public:
    MyListItem(const CEGUI::String& text) : ListboxTextItem(text)
    {
        setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush");
    }
};



// =============================================================================
// FxName
// -----------------------------------------------------------------------------
//! TODO: document
//!
//! \param [in] Arg
//!   TODO: document
//!
//! \retval Exception
//!   The success status of the requested operation.
//!   - eOKAY: Execution occurred as requested.
//!   .
//!
//! \b Example: \code
//! FxName(0);
//! \endcode
// -----------------------------------------------------------------------------
WindowDice::WindowDice(CEGUI::Window *pParent)
{
  // If no parent was requested, we'll attach to the root.
  if (pParent == NULL)
    { pParent = GuiCEGUI::getRoot(); }

  // Store our parent for future reference.
  m_pParent = pParent;

  // Load the console window layout and add it to the parent.
  pParent->addChildWindow(GuiCEGUI::getWm()->loadWindowLayout("dq_dice.layout"));

  // TODO: stuff...

  Window* pWindow = static_cast<Window*>(GuiCEGUI::getWm()->getWindow("Dice"));

  pWindow->setPosition(UVector2(UDim(0.4f, 0.0f), UDim(0.05f, 0.0f)));
//  pWindow->setPosition(UVector2(UDim(0.01f, 0.0f), UDim(0.05f, 0.0f)));

  Combobox* cbobox = static_cast<Combobox*>(GuiCEGUI::getWm()->getWindow("Dice/Combobox"));
  // add items to the combobox list
  cbobox->addItem(new MyListItem("1d6"));
  cbobox->addItem(new MyListItem("1d8"));
  cbobox->addItem(new MyListItem("1d10"));
  cbobox->addItem(new MyListItem("1d12"));
  cbobox->addItem(new MyListItem("1d20"));
  cbobox->addItem(new MyListItem("1d100"));
  cbobox->addItem(new MyListItem("1d6+1"));
  cbobox->addItem(new MyListItem("1d6+2"));
  cbobox->addItem(new MyListItem("1d8+1"));
  cbobox->addItem(new MyListItem("1d8+2"));

  // Roll button.
  subscribeEvent("Dice/Roll", PushButton::EventClicked, SubscriberSlot(&eventRoll));

//
//  ButtonBase* pButton = static_cast<ButtonBase*>(GuiCEGUI::getWm()->getWindow("Dice/Roll"));
////  Button* pButton = static_cast<Button*>(GuiCEGUI::getWm()->getWindow("Dice/Roll"));
//
//  //subscribeEvent("Dice/Roll", PushButton::EventClicked, SubscriberSlot(&eventRoll));
//  subscribeEvent("Dice/Roll", PushButton::EventClicked, pButton);


//  // Clear button.
//  subscribeEvent("Console/Clear", PushButton::EventClicked, SubscriberSlot(&eventClear));

}

//  WindowDice::~WindowDice()
//  {
//    // TODO Auto-generated destructor stub
//  }

//
//
//  // =============================================================================
//  // FxName
//  // -----------------------------------------------------------------------------
//  //! TODO: document
//  //!
//  //! \param [in] Arg
//  //!   TODO: document
//  //!
//  //! \retval Exception
//  //!   The success status of the requested operation.
//  //!   - eOKAY: Execution occurred as requested.
//  //!   .
//  //!
//  //! \b Example: \code
//  //! FxName(0);
//  //! \endcode
//  // -----------------------------------------------------------------------------
//  tERROR WindowDice::create(Window *pParent)
//  {
//  //  Window *pWindow;
//
//  // TODO: resolve this issue
//  //  // We can only create one dice window.
//  //  if (createSingle(pParent) == false)
//  //    { return (ERR_GUI_UNABLE_TO_CREATE); }
//
//    // If no parent was provided, we will attach it to the root.
//    if (pParent == NULL) { pParent = GuiCEGUI::getRoot(); }
//
//    // Load the console window layout and add it to the parent.
//    pParent->addChildWindow(GuiCEGUI::getWm()->loadWindowLayout("dq_dice.layout"));
//
//    Window* pWindow = static_cast<Window*>(GuiCEGUI::getWm()->getWindow("Dice"));
//
//    pWindow->setPosition(UVector2(UDim(0.25f, 0.0f), UDim(0.25f, 0.0f)));
//
//    Combobox* cbobox = static_cast<Combobox*>(GuiCEGUI::getWm()->getWindow("Dice/Combobox"));
//    // add items to the combobox list
//    cbobox->addItem(new MyListItem("1d6"));
//    cbobox->addItem(new MyListItem("1d8"));
//    cbobox->addItem(new MyListItem("2d8"));
//    cbobox->addItem(new MyListItem("1d20"));
//    cbobox->addItem(new MyListItem("1d6+2"));
//    cbobox->addItem(new MyListItem("1d6+1d8"));
//
//
//    // Roll button.
//    subscribeEvent("Dice/Roll", PushButton::EventClicked, SubscriberSlot(&eventRoll));
//
//  //  // Clear button.
//  //  subscribeEvent("Console/Clear", PushButton::EventClicked, SubscriberSlot(&eventClear));
//
//    // Return success.
//    return (ERR_NONE);
//  }


bool WindowDice::eventRoll(const CEGUI::EventArgs& Event)
{
  int Value, Min, Max;
  char Msg[100];

  Window *pRoot = GuiCEGUI::getRoot();

//  Event.EventArgs

  // Get the windows combobox.
  Window* pParent = static_cast<Window*>(pRoot->getChild("Dice"));
  Combobox *pComboBox = static_cast<Combobox*>(pParent->getChild("Dice/Combobox"));

  //Combobox *pComboBox = static_cast<Combobox*>(m_pParent->getChild("Dice/Combobox"));

  // Get the input text from the edit box.
  String Text(pComboBox->getText());

  // Get the values from the dice.
  Value = Dice::getSingleton()->roll((char*)Text.c_str());
  Min = Dice::getSingleton()->roll((char*)Text.c_str(), eROLL_MINIMUM);
  Max = Dice::getSingleton()->roll((char*)Text.c_str(), eROLL_MAXIMUM);

  sprintf(&(Msg[0]), "Min= %d | Value= %d | Max = %d", Min, Value, Max);
  WindowConsole::getSingleton().appendText(&(Msg[0]));


//    Window *pRoot = GuiCEGUI::getRoot();
//
//    // Get the edit box that is providing the text input.
//    Window* pParent = static_cast<Window*>(pRoot->getChild("Console"));
//  //  Editbox* pEditBox = static_cast<Editbox*>(pRoot->getChild("Console/Input"));
//    Editbox* pEditBox = static_cast<Editbox*>(pParent->getChild("Console/Input"));
//
//    // Get the input text from the edit box.
//    String Text(pEditBox->getText());



//  char Msg[100];
//  int Result;
//
//  bool Low = false;
//  bool High = false;
//
//  while ((!Low) || (!High))
//  {
//    Result = Dice::getSingleton()->roll("1d20");
//    sprintf(&(Msg[0]), "Roll: %d expected 2 to 12", Result);
//    WindowConsole::getSingleton().appendText(&(Msg[0]));
//
//    if (Result == 1) Low = true;
//    if (Result == 20) High = true;
//  }

//  Result = Dice::getSingletonPtr()->roll("2d6+2");
//  sprintf(&(Msg[0]), "Roll: %d expected 14", Result);
//  WindowConsole::getSingleton().appendText(&(Msg[0]));
//
//  Result = Dice::getSingletonPtr()->roll("(2d6)+2");
//  sprintf(&(Msg[0]), "Roll: %d expected 14", Result);
//  WindowConsole::getSingleton().appendText(&(Msg[0]));
//
//  Result = Dice::getSingletonPtr()->roll("(2d6)+2d20+6-1d10");
//  sprintf(&(Msg[0]), "Roll: %d expected 48", Result);
//  WindowConsole::getSingleton().appendText(&(Msg[0]));
//
//  Result = Dice::getSingletonPtr()->roll("2d10*1d4");
//  sprintf(&(Msg[0]), "Roll: %d expected 80", Result);
//  WindowConsole::getSingleton().appendText(&(Msg[0]));
//
//  Result = Dice::getSingletonPtr()->roll("(2d10)*(1d4)");
//  sprintf(&(Msg[0]), "Roll: %d expected 80", Result);
//  WindowConsole::getSingleton().appendText(&(Msg[0]));

  // Return the event was handled.
  return (true);
}



